<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>03列表过滤</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="app">
    <h1>{{title}}</h1>
    <input type="text" placeholder="请输入名字" v-model="keyword">
    <ul>
      <li v-for="p in filterPersons">{{p.name}}-{{p.age}}</li>
    </ul>
  </div>
  <script>
    new Vue({
      el: '#app',
      data(){
        return {
          title: '列表过滤',
          persons: [
            {
              id: '001',
              name: '马冬梅',
              age: 18
            },
            {
              id: '002',
              name: '周冬雨',
              age: 18
            },
            {
              id: '003',
              name: '周杰伦',
              age: 18
            },
            {
              id: '004',
              name: '温兆伦',
              age: 18
            }
          ],
          keyword: '',
          // filterPersons: []
        }
      },
      // 1 用watch实现
      /* watch: {
        keyword: {
          immediate: true,
          handler(val) {
            const newArr = this.persons.filter(item => item.name.indexOf(val) !== -1)
            this.filterPersons = newArr
          }
        }
      } */

      // 2 用computed实现
      computed: {
        filterPersons() {
          return this.persons.filter(item => item.name.indexOf(this.keyword) !== -1)
        }
      }
    })
  </script>
</body>
</html>